﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using TrippleGameDAL;
using TrippleGameChallange;
using System.Transactions;

namespace TrippleGameService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "TrippleGameService" in code, svc and config file together.
    public class TrippleGameService : ITrippleGameService
    {
        const int STARTING_NUMBER_OF_GAMES = 10;
        static List<GameDTO> listOfGames = new List<GameDTO>(STARTING_NUMBER_OF_GAMES);

        public void CreateNewGame(string name)
        {
            TrippleGame.CreateGame(name);
        }

        public char JoinGame(GameJoiner gameJoiner)
        {
                TripleGameEntities entities = new TripleGameEntities();
                GameDTO selectedGame = listOfGames.FirstOrDefault(g => g.Id == gameJoiner.GameId);
                char letter = '0';
                Game game = TrippleGame.GetGame(gameJoiner.GameId, entities);

                if (selectedGame == null)
                {
                    selectedGame = new GameDTO()
                   {
                       Id = gameJoiner.GameId,
                       Player1Id = gameJoiner.PlayerId,
                   };
                    letter = 'a';
                    TrippleGame.JoinGame(Guid.Parse(selectedGame.Player1Id), entities, game, letter);
                    listOfGames.Add(selectedGame);
                }
                else
                {
                    if (selectedGame.Player1Id == gameJoiner.PlayerId)
                    {
                        return TrippleGame.P1;
                    }

                    if (selectedGame.Player2Id == null)
                    {
                        selectedGame.Player2Id = gameJoiner.PlayerId;
                        letter = 'b';
                        TrippleGame.JoinGame(Guid.Parse(selectedGame.Player2Id), entities, game, letter);
                    }
                }
                entities.SaveChanges();

                return letter;
        }

        public string Move(GameMoveContractor moveContractor)
        {
            TripleGameEntities entities = new TripleGameEntities();
            Game game = TrippleGame.GetGame(moveContractor.GameID, entities);
            string result = TrippleGame.PerformMove(moveContractor.StartY, moveContractor.StartX,
                moveContractor.EndY, moveContractor.EndX, moveContractor.PlayerSymbol, game.Matrix);

            TrippleGame.UpdateDatabase(game, result, entities);

            return result;
        }

        public List<GameDTO> ShowAvailableGames()
        {
            List<Game> games= TrippleGame.LoadGames();
            List<GameDTO> gamesConverted = GameDTO.ConvertAllFromGame(games);
            return gamesConverted;
        }

        public string GetNextPlayer(NextPlayerContractor contractor)
        {
           return TrippleGame.WhichPlayerTurn(contractor.PlayerLetter, contractor.Matrix, contractor.GameId);
        }

        public UpdateContract Update(string gameId)
        {
            TripleGameEntities entities = new TripleGameEntities();
            int gameIdNumber = int.Parse(gameId);
            Game game = TrippleGame.GetGame(gameIdNumber, entities);
            UpdateContract contractor = new UpdateContract();
            contractor.NextPlayerSymbol = game.PlayerTurn;
            contractor.Matrix = game.Matrix;
            contractor.GameState = game.State;
            return contractor;
        }


        public string CreatePlayer(string name)
        {
            Player player = TrippleGame.CreatePlayer(name);
            
            return player.PlayerId.ToString();
        }

    }
}
